﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using GenaralLibs;

namespace Lists
{
    public class XuLyViPham : DoiTuong
    {
        #region int ID property
        public int ID
        {
            get
            {
                return (int)_DataRow["ID"];
            }
            set
            {
                _DataRow["ID"] = value;
            }
        }
        #endregion

        #region int HoSoID property
        public int HoSoID
        {
            get
            {
                return (int)_DataRow["HoSoID"];
            }
            set
            {
                _DataRow["HoSoID"] = value;
            }
        }
        #endregion

        #region int SoTT property
        public int SoTT
        {
            get
            {
                return (int)_DataRow["SoTT"];
            }
            set
            {
                _DataRow["SoTT"] = value;
            }
        }
        #endregion

        #region DateTime NgayXuLy property
        public DateTime NgayXuLy
        {
            get
            {
                return (DateTime)_DataRow["NgayXuLy"];
            }
            set
            {
                _DataRow["NgayXuLy"] = value;
            }
        }
        #endregion

        #region string HanhViiPham property
        public string HanhViiPham
        {
            get
            {
                return (string)_DataRow["HanhViiPham"];
            }
            set
            {
                _DataRow["HanhViiPham"] = value;
            }
        }
        #endregion

        #region string DonViXuLy property
        public string DonViXuLy
        {
            get
            {
                return (string)_DataRow["DonViXuLy"];
            }
            set
            {
                _DataRow["DonViXuLy"] = value;
            }
        }
        #endregion

        #region int MucXuPhatID property
        public int MucXuPhatID
        {
            get
            {
                return (int)_DataRow["MucXuPhatID"];
            }
            set
            {
                _DataRow["MucXuPhatID"] = value;
            }
        }
        #endregion
        #region DMMucXuPhat property
        public DMMucXuPhat DMMucXuPhat
        {
            get
            {
                DMMucXuPhat dm = DanhMucMucXuPhat.Instance.GetByID((int)_DataRow["MucXuPhatID"]);
                return (DMMucXuPhat)dm;
            }
            set
            {
                _DataRow["MucXuPhatID"] = value;
            }
        }
        #endregion

        #region string GhiChu property
        public string GhiChu
        {
            get
            {
                return (string)_DataRow["GhiChu"];
            }
            set
            {
                _DataRow["GhiChu"] = value;
            }
        }
        #endregion

        public XuLyViPham(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucXuLyViPham : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucXuLyViPham _Instance;
        public static DanhMucXuLyViPham Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucXuLyViPham();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "XuLyViPham";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new XuLyViPham((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public XuLyViPham this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new XuLyViPham(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public XuLyViPham NewDoiTuong()
        {
            try
            {
                return new XuLyViPham(Instance.DataTable.NewRow());
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public XuLyViPham GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new XuLyViPham(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public XuLyViPham GetByID(int objID)
        {
            try
            {
                return GetByProperty("ID", objID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DanhMucXuLyViPham GetByHoSoID(int hoSoID)
        {
            try
            {
                Instance.DataView.RowFilter = string.Format("HoSoID = {0}", hoSoID);
                return Instance;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucXuLyViPham GetAll()
        {
            try
            {
                return Instance;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}